﻿using Gtkj.CMS.Model;
using Gtkj.CMS.Model.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace Gtkj.CMS.BLL
{
    public partial class PhoneMessageService : BaseService<PhoneMessage>
    {

        static readonly string Uid = System.Configuration.ConfigurationManager.AppSettings["PhoneMsgUid"];
        static readonly string Key = System.Configuration.ConfigurationManager.AppSettings["PhoneMsgKey"];
        /// <summary>
        /// 发送注册短信
        /// </summary>
        /// <param name="phoneMessage">传入手机号,短信类型</param>
        /// <param name="error">返回错误信息</param>
        /// <returns></returns>
        public string SendMessage(PhoneMessage phoneMessage, out string error)
        {
            error = "";
            DateTime dt = DateTime.Now.Date;
            var temp = this.CurrentDal.LoadEntities(x => x.PhoneNum.Equals(phoneMessage.PhoneNum) && x.CreateTime >= dt);
            if (temp.Count() > 20)
            {
                error = "当天发送短信已达上限,请明天再试!";
                return null;
            }
            PhoneMessage Message = temp.OrderByDescending(x => x.MessageID).FirstOrDefault();
            if (phoneMessage != null)
            {
                if (phoneMessage.CreateTime > DateTime.Now.AddMinutes(-1))
                {
                    error = "1分钟内只允许发送1条短信!";
                    return null;
                }
            }
                
            Random radom = new Random();
            int rnd = radom.Next(100000, 999999);

            phoneMessage.Code = rnd.ToString();
            string url = "http://utf8.sms.webchinese.cn/?Uid="+Uid+"&Key="+Key+"&smsMob="+phoneMessage.PhoneNum + "&smsText=";
            string Context="";
            switch ((PhoneMsgTypeEnum)phoneMessage.Type)
            {
                case PhoneMsgTypeEnum.修改绑定手机号:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.修改绑定手机号 .ToString()+ "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.修改银行卡:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.修改银行卡.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.验证原手机号码:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.验证原手机号码.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.修改密码:
                    {
                        //int count = this.GetCurrentSession.UserInfoDAL.LoadEntities(a => a.Phone == phoneMessage.PhoneNum && !a.IsDelete).Count();
                        //if(count>0)
                        //{
                        //    error = "此手机号已被注册!";
                        //    return false;
                        //}

                        Context = "您正在进行" + PhoneMsgTypeEnum.修改密码.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.用户注册:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.用户注册.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.找回支付密码:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.找回支付密码.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.修改支付宝:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.修改支付宝.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.提现:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.提现.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.找回密码:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.找回密码.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.扶持金申请:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.扶持金申请.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.发送余额:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.发送余额.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                case PhoneMsgTypeEnum.发送佣金:
                    {
                        Context = "您正在进行" + PhoneMsgTypeEnum.发送佣金.ToString() + "的验证码操作，需输入验证码：" + phoneMessage.Code + "，请您在60秒内完成操作，泄露验证码会影响资金安全，切勿告知他人！";
                        break;
                    }
                default: return null;
            }
            url+=Context;
            string result= Common.WebHelper.DoGet(url);
            if ( result != "1")
            {
                error = "发送失败,请稍后再试";
                string e = "";
                switch (result)
                {
                    case "-1": e = "没有该用户账户;"; break;
                    case "-2": e = "接口密钥不正确;"; break;
                    case "-3": e = "短信数量不足;"; break;
                    case "-11": e = "该用户被禁用;"; break;
                    case "-14": e = "短信内容出现非法字符;"; break;
                    case "-4": e = "手机号格式不正确;"; break;
                    case "-42": e = "短信内容为空;"; break;
                    case "-51": e = "短信签名格式不正确;"; break;
                    case "-6": e = "IP限制;"; break;
                    default: return null;
                }
                Common.LogHelper.WriteLogFile("Gtkj.CMS.BLL.PhoneMessageService.SendMessage:" + e);
                return null;
            }

            phoneMessage.CreateTime = DateTime.Now;
            phoneMessage.Context = Context;
            phoneMessage.State = 0;
            this.GetCurrentSession.PhoneMessageDAL.AddEntity(phoneMessage);
            this.GetCurrentSession.SaveChange();
            return phoneMessage.Code;
        }

    }
}
